package com.lizhi.note.service.impl;

import com.lizhi.note.entity.NotepageHistoryEntity;
import com.lizhi.note.repository.NotepageHistoryRepository;
import com.lizhi.note.service.NotepageHistoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class NotepageHistoryServiceImpl implements NotepageHistoryService {

    private final NotepageHistoryRepository repository;

    public NotepageHistoryServiceImpl(NotepageHistoryRepository repository) {
        this.repository = repository;
    }

    @Override
    public NotepageHistoryEntity create(NotepageHistoryEntity entity) {
        return repository.save(entity);
    }

    @Override
    public Optional<NotepageHistoryEntity> getById(Long id) {
        return repository.findById(id);
    }

    @Override
    public List<NotepageHistoryEntity> getAllByNotePageId(Long notePageId) {
        return repository.findByNotePageIdOrderByUpdateTimeDesc(notePageId);
    }

    @Override
    public List<NotepageHistoryEntity> getAllByNotebookId(Long notebookId) {
        return repository.findByNotebookId(notebookId);
    }

    @Override
    public NotepageHistoryEntity update(NotepageHistoryEntity entity) {
        if (!repository.existsById(entity.getId())) {
            throw new IllegalArgumentException("笔记历史记录不存在，ID: " + entity.getId());
        }
        return repository.save(entity);
    }

    @Override
    public void delete(Long id) {
        repository.deleteById(id);
    }
}
